home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / 140_01.zip / DATEDEMO.C < prev    next >
Text File  |  1993-06-26  |  7KB  |  325 lines

  1. #include    CLOCK.H
  2.  
  3. #define    CENTURY    19        /* 20th Century at the moment */
  4.  
  5. /* Example program for real time system clocks.
  6.  
  7.    This main program simply calls the date function and time function,
  8.    displaying the returned strings.
  9.  
  10.    By Bill Bolton,
  11.    Software Tools,
  12.    P.O. Box 80,
  13.    Newport Beach,
  14.    NSW, 2106,
  15.    AUSTRALIA
  16.  
  17.    Source Address TCY-396
  18.    Phone (+61 2) 997-1018
  19.  
  20.    Version 1.0 for Mountain Hardware 100,000 day clock 11/5/81
  21.    Version 1.1 for Godbout Systsen Support 1 clock 19/1/82 */
  22.  
  23. main()
  24.  
  25. {
  26.     char    date_str[35];    /* string (character array) for date */
  27.     char    time_str[8];    /* string for time */
  28.  
  29.     if (date(date_str,0)){
  30.         printf("Exiting to CP/M\07\n");
  31.         exit();
  32.     }
  33.     printf("Demonstration of 'C' functions for the ");
  34.     printf("Godbout System Support 1 clock.\n\n");
  35.     printf("\tBy Bill Bolton, Software Tools\n");
  36.     printf("\tP.O. Box 80,\n\tNewport Beach,\n");
  37.     printf("\tNSW, 2106, AUSTRALIA\n\n");
  38.     printf("\Date format 0 is %s\n",date_str);
  39.     date(date_str,1);
  40.     printf("Date format 1 is %s\n",date_str);
  41.     date(date_str,2);
  42.     printf("Date format 2 is %s\n",date_str);
  43.     date(date_str,3);
  44.     printf("Date format 3 is %s\n",date_str);
  45.     date(date_str,4);
  46.     printf("Date format 4 is %s\n",date_str);
  47.     date(date_str,5);
  48.     printf("Date format 5 is %s\n",date_str);
  49.     date(date_str,6);
  50.  
  51.     time(time_str,0);
  52.     printf("Time of day format 0 = %s\n",time_str);
  53.     time(time_str,1);
  54.     printf("Time of day format 1 = %s\n",time_str);
  55.     time(time_str,2);
  56.     printf("Time of day format 2 = %s\n",time_str);
  57.     time(time_str,3);
  58.     date(date_str,1);
  59.     time(time_str,0);
  60.     printf("Printed at %s Hours on %s.\n\n",
  61.             time_str,date_str);
  62.  }
  63.  
  64. /* date(srt,format) will fill a string with date formatted as follows:
  65.  
  66.     format = 0    "May 11, 1981"
  67.     format = 1    "Monday, May 11, 1981"
  68.     format = 2    "11/5/1981"
  69.     format = 3    "Monday, 11/5/1981"
  70.     format = 4    "11/5/81"
  71.     format = 5    "Monday, 11/5/81"
  72. */
  73.  
  74. date(str,format)
  75.  
  76. char    *str;        /* pointer to date string */
  77. int    format;        /* format identifier */
  78.  
  79. {
  80.     char    wname[10];    /* string for week day proper name */
  81.     char    mname[12];    /* string for month proper name */
  82.     int    year[1];    /* year, range 1978 to ????? */
  83.     int    month[1];    /* month of the year, range 1 to 12 */
  84.     int    mday[1];    /* day of the current month, range 0 to 31 */
  85.     int    wday[1];    /* day of the week, range 0 to 6 */ 
  86.  
  87.     if (get_date(year,month,mday,wday)){
  88.         printf("No clock board present in system\07\n");
  89.         return(-1);
  90.     } 
  91.     name_month(mname,month);
  92.     name_week(wname,wday);
  93.     switch(format){
  94.     case 0:
  95.         sprintf(str,"%s %d, %d%d",mname,*mday,CENTURY,*year);
  96.         return(0);
  97.     case 1:
  98.         sprintf(str,"%s, %s %d, %d%d",wname,mname,*mday,CENTURY,*year);
  99.         return(0);
  100.     case 2:
  101.         sprintf(str,"%d/%d/%d%d",*mday,*month,CENTURY,*year);
  102.         return(0);
  103.     case 3:
  104.         sprintf(str,"%s, %d/%d/%d%d",wname,*mday,*month,CENTURY,*year);
  105.         return(0);
  106.     case 4:
  107.         sprintf(str,"%d/%d/%d",*mday,*month,*year);
  108.         return(0);
  109.     case 5:
  110.         sprintf(str,"%s, %d/%d/%d",wname,*mday,*month,*year);
  111.         return(0);
  112.     default:
  113.         printf("Date format argument ERROR !\07\n\n");
  114.         return(-1);
  115.     }
  116. }
  117.  
  118. /* get_date(year,month,mday,wday) provides the basic data for formatting
  119.    a date string, fetched from the clock board and converted to a useable
  120.    set of values */
  121.  
  122. int get_date(year,month,mday,wday)
  123.  
  124. int    *year;        /* pointer to current year */
  125. int    *month;        /* pointer to current month */
  126. int    *mday;        /* pointer to day of the month */
  127. int    *wday;        /* pointer to day of the week */
  128.  
  129. {
  130.     if (inp(CDATA) == 0XFF )      /* no clock board present */
  131.         return(-1);
  132.     *year =  (read_digit(YEAR10) * 10) + read_digit(YEAR1);
  133.     *month = ((read_digit(MONTH10) & 1) * 10) + read_digit(MONTH1);
  134.     *mday = ((read_digit(DAY10) & 3) * 10) + read_digit(DAY1);
  135.     *wday = read_digit(WDAY);
  136.     outp(CLKCMD,0);
  137.     return(0);
  138. }
  139.  
  140.  
  141. int read_digit(address)
  142.  
  143. int    address;
  144.  
  145. {
  146.     int    instruct;
  147.  
  148.     instruct = address + CREAD;
  149.     outp(CLKCMD,instruct);
  150.     return (inp(CDATA));
  151. }
  152.  
  153. /* ndays(year) returns the number of days in the current year.
  154. */
  155.  
  156. int ndays(year)
  157.  
  158. int    *year;        /* pointer to current year */
  159.  
  160. {
  161.     return(leap(*year) ?  366 : 365);
  162. }
  163.  
  164. /* leap(year) returns a flag to indicate if current year is a leap year.
  165. */
  166.  
  167. int leap(year)
  168.  
  169. int    year;        /* current year */
  170.  
  171. {
  172.     return (year%4 == 0 && year%100 != 0 || year%400 == 0);
  173. }
  174.  
  175. /* name_month(mname,month) fills a string with the name of the the
  176.    current month.
  177. */
  178.  
  179. int name_month(mname,month)
  180.  
  181. char    *mname;        /* pointer to month name string */
  182. int    *month;        /* pointer to current month */
  183.  
  184. {
  185.     switch(*month){
  186.     case 1:
  187.         strcpy(mname,"January");
  188.         return(0);
  189.     case 2:
  190.         strcpy(mname,"February");
  191.         return(0);
  192.     case 3:
  193.         strcpy(mname,"March");
  194.         return(0);
  195.     case 4:
  196.         strcpy(mname,"April");
  197.         return(0);
  198.     case 5:
  199.         strcpy(mname,"May");
  200.         return(0);
  201.     case 6:
  202.         strcpy(mname,"June");
  203.         return(0);
  204.     case 7:
  205.         strcpy(mname,"July");
  206.         return(0);
  207.     case 8:
  208.         strcpy(mname,"August");
  209.         return(0);
  210.     case 9:
  211.         strcpy(mname,"September");
  212.         return(0);
  213.     case 10:
  214.         strcpy(mname,"October");
  215.         return(0);
  216.     case 11:
  217.         strcpy(mname,"November");
  218.         return(0);
  219.     case 12:
  220.         strcpy(mname,"December");
  221.         return(0);
  222.     default:
  223.         printf("Month name ERROR ! Month was %d\n",*month);
  224.         return(-1);
  225.     }
  226. }
  227.  
  228. /* name_week(wname,wday) fills a string with the name of the current
  229.    week day.
  230. */
  231.  
  232. int name_week(wname,wday)
  233.  
  234. char    *wname;        /* pointer to week name string */
  235. int    *wday;        /* pointer to current week day */
  236.  
  237. {
  238.     switch(*wday){
  239.     case 0:
  240.         strcpy(wname,"Sunday");
  241.         return(0);
  242.     case 1:
  243.         strcpy(wname,"Monday");
  244.         return(0);
  245.     case 2:
  246.         strcpy(wname,"Tuesday");
  247.         return(0);
  248.     case 3:
  249.         strcpy(wname,"Wednesday");
  250.         return(0);
  251.     case 4:
  252.         strcpy(wname,"Thursday");
  253.         return(0);
  254.     case 5:
  255.         strcpy(wname,"Friday");
  256.         return(0);
  257.     case 6:
  258.         strcpy(wname,"Saturday");
  259.         return(0);
  260.     default:
  261.         printf("Weekday name ERROR !\n");
  262.         return(-1);
  263.     }
  264. }
  265.  
  266. /* time(str,format) fills a string with the time of day in the 
  267.    following formats :
  268.  
  269.     format 0     1800:15
  270.     format 1    18:00:15
  271.     format 2    18:00
  272. */
  273.  
  274. time(str,format)
  275.  
  276. char    *str;        /* string to fill with time */
  277. int    format;        /* flag for format of string */
  278.  
  279. {
  280.     int    t[6];
  281.  
  282.     if (read_clock(t)){
  283.         printf("No clock board present in system !\07\n");
  284.         return(-1);
  285.     } 
  286.     switch(format){
  287.     case 0:
  288.         sprintf(str,"%d%d%d%d:%d%d",t[0],t[1],t[2],t[3],t[4],t[5]);
  289.         return(0);
  290.     case 1:
  291.         sprintf(str,"%d%d:%d%d:%d%d",t[0],t[1],t[2],t[3],t[4],t[5]);
  292.         return(0);
  293.     case 2:
  294.         sprintf(str,"%d%d:%d%d",t[0],t[1],t[2],t[3]);
  295.         return(0);
  296.     default:
  297.         printf("Time of day format argument ERROR !\07\n\n");
  298.         return(-1);
  299.     }
  300. }
  301.  
  302. /* read_clock(t) fills an array with the time of day digits read from
  303.    the clock board
  304. */
  305.  
  306. int read_clock(t)
  307.  
  308. int    *t;        /* array to store clock digits */
  309.  
  310. {
  311.     int    ptr;    /* pointer into digit array */
  312.  
  313.     if (inp(CDATA) == 0XFF )    /* no clock board present */
  314.         return(-1);
  315.     t[0] = (read_digit(HOUR10) & 3);
  316.     t[1] = read_digit(HOUR1);
  317.     t[2] = (read_digit(MIN10) & 7);
  318.     t[3] = read_digit(MIN1);
  319.     t[4] = (read_digit(SEC10) & 7);
  320.     t[5] = read_digit(SEC1);
  321.     outp(CLKCMD,0);
  322.     return(0);
  323. }
  324.  
  325.